package com.wh.wisdomsite.xbox.server.controller.de;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.de.entity.Device;
import com.wh.wisdomsite.xbox.de.service.IDeviceService;
import com.wh.wisdomsite.xbox.pojo.de.DeviceIn;
import com.wh.wisdomsite.xbox.server.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;

/**
 * 设备Controller
 * 整合扬尘，起重机械，视频设备
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.server.controller.de
 * @author: 丁奕
 * @date: 2018-11-23 09:29
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@Controller
@RequestMapping(value = "/device")
public class DeviceController {

    private static final Log logger = LogFactory.getLog(DeviceController.class);

    @Autowired
    private IDeviceService iDeviceService;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 页面跳转
     * @return
     */
    @RequiresPermissions("rules:de_device_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(String deviceType,HttpServletRequest request){
        request.setAttribute("deviceType",deviceType);
        return "/de/device/deviceList";
    }

    /**
     * 获取设备集合
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:de_device_find")
    @RequestMapping(value = "/deviceList.do")
    public void deviceList(HttpServletResponse response, DeviceIn in){
        try {
            logger.info("加载设备数据列表-->>开始");
            //设置分页
            if(in.getPage() == 0) in.setPage(1);
            if(in.getRows() == 0) in.setRows(10);
            Page<Device> page = new Page<Device>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            EntityWrapper<Device> ewDevice = new EntityWrapper<Device>();
            ewDevice.setEntity(new Device());
            if(StringUtils.isNotEmpty(in.getPrName())){
                ewDevice.like("de.de_prName", in.getPrName());
            }
            if(StringUtils.isNotEmpty(in.getDeName())){
                ewDevice.like("de.de_deName", in.getDeName());
            }
            if(StringUtils.isNotEmpty(in.getDeviceId())){
                ewDevice.like("de.de_deviceId", in.getDeviceId());
            }
            if(StringUtils.isNotEmpty(in.getType())){
                ewDevice.eq("de.de_type", in.getType());
            }else{
                if(StringUtils.isNotEmpty(in.getDeviceType())){
                    ewDevice.eq("de.de_type", in.getDeviceType());
                }
            }
            //若用户不是超级管理员根据用户的数据权限添加查询条件
            if(!UserUtils.isSysAdmin()){
                ewDevice = UserUtils.setEntityWrapper(ewDevice, "co", "off", "pr");
            }
            ewDevice.orderBy("de.de_createDate", false);
            Page<Device> devicePage = iDeviceService.getPage(page, ewDevice);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if(devicePage.getRecords().size() > 0){
                for(Device device : devicePage.getRecords()){
                    JSONObject object = new JSONObject(device);
                    if(StringUtils.isNotEmpty(device.getCreateDate().toString())){
                        object.put("createDate", simpleDateFormat.format(device.getCreateDate()));
                    }
                    array.put(object);
                }
            }
            json.put("total",devicePage.getTotal());
            json.put("rows",array);
            ResManager.getTextOut(response).print(json);
            logger.info("加载设备数据列表-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("加载设备数据列表-->>Exception" + e.getMessage());
        }
    }

    /**
     * 设备详情
     * @param deviceIn
     * @param request
     * @return
     */
    @RequiresPermissions("rules:de_device_view")
    @RequestMapping("/deviceDesc.do")
    public String deviceDesc(DeviceIn deviceIn, HttpServletRequest request) {
        logger.info("设备详情-->>开始");
        if(StringUtils.isNotEmpty(deviceIn.getId()) && StringUtils.isNotEmpty(deviceIn.getType())){
            if(deviceIn.getType().equals("dust")){
                return "redirect:/dustProCheckPoint/dustProCheckPointDesc.do?id=" + deviceIn.getId() + "&source=deviceManage";
            }
            if(deviceIn.getType().equals("crane")){
                return "redirect:/craneProject/craneProjectDesc.do?cpId=" + deviceIn.getId() + "&source=deviceManage";
            }
            if(deviceIn.getType().equals("video")){
                return "redirect:/videoProject/videoProjectDesc.do?id=" + deviceIn.getId() + "&source=deviceManage";
            }
            logger.info("设备详情-->>结束");
        }
        return null;
    }
}
